package com.hardy.node;

/**
 * @author fuxiaohui3@nuoyuan.com.cn
 * @date 2018/11/21 10:18
 * 单链表反转
 */
public class ReverseLinkedList {
    public static void main(String[] args) {
        LinkedNode head = new LinkedNode(1);
        head.next = new LinkedNode(2);
        head.next.next = new LinkedNode(3);
        head.next.next.next = new LinkedNode(4);
        head.next.next.next.next = new LinkedNode(5);
        LinkedNode next = head;
        while (next!=null){
            System.out.print(next.value);
            next = next.next;
        }
        ReverseLinkedList reverseLinkedList = new ReverseLinkedList();
        head = reverseLinkedList.reverse(head);
        LinkedNode next1 = head;
        while (next1!=null){
            System.out.print(next1.value);
            next1 = next1.next;
        }
    }

    public LinkedNode reverse(LinkedNode head){
        LinkedNode next = head;
        LinkedNode pre = null;
        while (next!=null){
            next = head.next;
            head.next= pre;
            pre = head;
            head = next;
        }
        return pre;
    }
}
